package com.echo.im.client.handler.message.suport;


import com.echo.im.client.handler.message.AbstractMessageHandler;
import com.echo.im.client.helper.OnlineStatusHelper;
import com.echo.im.common.enums.IMCmdType;
import com.echo.im.common.model.IMRecvInfo;
import com.echo.im.common.model.IMUserInfo;
import com.echo.im.common.model.message.IMSystemMessage;
import com.echo.im.common.model.mq.OfflineMsgMqInfo;
import com.echo.im.common.vo.message.SystemMessageVO;
import com.echo.im.mq.event.model.OfflineMessageEvent;
import com.echo.im.mq.event.producer.EventPublisher;
import com.echo.im.mq.event.model.SystemPushMessageEvent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author: echo
 * @time: 2024-08-14
 */
@Component
public class SystemMessageHandler extends AbstractMessageHandler<IMSystemMessage, SystemMessageVO> {

    private final String appName;
    private final EventPublisher eventPublisher;
    private final OfflineMessageEvent offlineMessageEvent;
    private final SystemPushMessageEvent systemPushMessageEvent;


    public SystemMessageHandler(OnlineStatusHelper onlineStatusHelper,
                                @Value("${spring.application.name}")
                                String appName, EventPublisher eventPublisher, OfflineMessageEvent offlineMessageEvent,
                                SystemPushMessageEvent systemPushMessageEvent){
        super(onlineStatusHelper, appName);
        this.appName = appName;
        this.eventPublisher = eventPublisher;
        this.offlineMessageEvent = offlineMessageEvent;
        this.systemPushMessageEvent = systemPushMessageEvent;
    }

    @Override
    public void handleMessage(IMSystemMessage message) {
        HashSet<Long> offlineUserIds = new HashSet<>();
        Map<Integer, List<IMUserInfo>> serverIdUserInfoMap = onlineStatusHelper.getServerIdUserInfoMap(message.getRecvIds(),offlineUserIds );

        // 逐个server发送
        for (Map.Entry<Integer, List<IMUserInfo>> entry : serverIdUserInfoMap.entrySet()) {
            IMRecvInfo<SystemMessageVO> recvInfo = new IMRecvInfo<>();
            recvInfo.setCmd(IMCmdType.SYSTEM_MESSAGE.code());
            recvInfo.setReceivers(new LinkedList<>(entry.getValue()));
            recvInfo.setServiceName(appName);
            recvInfo.setSendResult(message.getSendResult());
            recvInfo.setData(message.getData());
            // 推送至队列
            eventPublisher.publish(systemPushMessageEvent.topic(), systemPushMessageEvent.buildEventMessage(recvInfo));
        }

        // 对每个离线用户分别投递离线系统消息
        if (!offlineUserIds.isEmpty()) {
            for (Long offlineUserId : offlineUserIds) {
                OfflineMsgMqInfo offlineMsgMqInfo = new OfflineMsgMqInfo(offlineUserId, message.getData().getId(),OfflineMsgMqInfo.OfflineMsgEnum.SYSTEM_OFFLINE_MESSAGE);
                eventPublisher.publish(offlineMessageEvent.topic(), offlineMessageEvent.buildEventMessage(offlineMsgMqInfo));
            }
        }
    }
}
